package com.example.ywj.toutiao;


import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.ywj.toutiao.bean.Comment;
import com.example.ywj.toutiao.bean.News;
import com.example.ywj.toutiao.utils.Constant;
import com.example.ywj.toutiao.utils.MyCommentBaseAdapter;
import com.example.ywj.toutiao.utils.MyEmojiAdapter;
import com.example.ywj.toutiao.utils.ResponseApi;
import com.example.ywj.toutiao.utils.SpUtils;
import com.example.ywj.toutiao.utils.Util;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import es.dmoral.toasty.Toasty;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class DetailActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "DetailActivity";
    @BindView(R.id.detail_search)
    public TextView tv_search;
    @BindView(R.id.detail_share)
    public TextView tv_share;
    @BindView(R.id.detail_back)
    public ImageView iv_back;
    @BindView(R.id.detail_pen)
    public TextView detail_pen;
    @BindView(R.id.detail_comment)
    public TextView detail_comment;
    @BindView(R.id.detail_save)
    public TextView detail_save;
    @BindView(R.id.detail_like)
    public TextView detail_like;
    @BindView(R.id.detail_content_detail)
    public TextView detail_conetent_title;
    @BindView(R.id.detail_user)
    public TextView detail_user;
    @BindView(R.id.detail_time)
    public TextView detail_time;
    @BindView(R.id.detail_avatar)
    public ImageView detail_avatar;
    @BindView(R.id.detail_content_layout)
    public LinearLayout linearLayout;
    @BindView(R.id.detail_scrollview)
    public ScrollView scrollView;
    @BindView(R.id.detail_title)
    public TextView detail_title;
    @BindView(R.id.detail_avatar1)
    public ImageView detail_avatar1;
    @BindView(R.id.detail_author)
    public TextView detail_aurhor;
    @BindView(R.id.detail_fans)
    public TextView detail_fans;
    @BindView(R.id.detail_attention1)
    public Button btn_attention1;
    @BindView(R.id.detail_attention)
    public Button btn_attention;
    @BindView(R.id.detail_et)
    public EditText detail_et;
    @BindView(R.id.edittext_hasfoucs)
    public LinearLayout edittext_hasfoucs;
    @BindView(R.id.edittext_nofoucs)
    public LinearLayout edittext_nofoucs;
    @BindView(R.id.detail_comment_btn)
    public Button btn_comment;
    @BindView(R.id.detail_bianqing)
    public TextView detail_bianqing;
    @BindView(R.id.detail_bianqing_panel)
    public LinearLayout detail_bianqing_panel;
    @BindView(R.id.detail_biaoqing_gridview)
    public GridView detail_biaoqing_gridview;
    @BindView(R.id.detail_comment_list)
    public ListView detail_comment_list;
    @BindView(R.id.detail_no_comment)
    public TextView detail_no_comment;
    @BindView(R.id.detail_view)
    public View detail_view;
    public String baseUrl = Constant.BASEURL;
    public List<News> list;
    public String username;
    public Boolean flag;
    public String attentionUser;
    public Boolean flag1;
    public Boolean flag2;
    public Typeface font;
    public String id;
    public Boolean flag3 = true;
    public List<Comment> commentList = new ArrayList<>();
    public List<Comment> newList2 = new ArrayList<>();
    public int emojis[] = {R.string.emoji1,R.string.emoji2,R.string.emoji3,R.string.emoji4,R.string.emoji5,R.string.emoji6,R.string.emoji7,R.string.emoji8,R.string.emoji9,R.string.emoji33,R.string.emoji10,R.string.emoji11,R.string.emoji12,R.string.emoji13,R.string.emoji14,R.string.emoji15,R.string.emoji16,R.string.emoji17,R.string.emoji18,R.string.emoji19,R.string.emoji20,R.string.emoji21,R.string.emoji22,R.string.emoji23,R.string.emoji24,R.string.emoji25,R.string.emoji26,R.string.emoji27,R.string.emoji28,R.string.emoji29,R.string.emoji30,R.string.emoji31,R.string.emoji32,R.string.emoji34,R.string.emoji35,R.string.emoji36,R.string.emoji37,R.string.emoji38,R.string.emoji39,R.string.emoji40,R.string.emoji41,R.string.emoji42,R.string.emoji43,R.string.emoji44,R.string.emoji45,R.string.emoji46,R.string.emoji47,R.string.emoji48,R.string.emoji49,R.string.emoji50,R.string.emoji51,R.string.emoji52,R.string.emoji53,R.string.emoji54,R.string.emoji55,R.string.emoji56,R.string.emoji57,R.string.emoji58,R.string.emoji59,R.string.emoji60,R.string.emoji61,R.string.emoji62,R.string.emoji63,R.string.emoji64,R.string.emoji65,R.string.emoji66,R.string.emoji67,R.string.emoji68,R.string.emoji69,R.string.emoji70,R.string.emoji71,R.string.emoji72,R.string.emoji73,R.string.emoji74,R.string.emoji75,R.string.emoji76,R.string.emoji77,R.string.emoji78,R.string.emoji79,R.string.emoji80};
    public List<Integer> listEmoji = new ArrayList<>();
    public MyCommentBaseAdapter myCommentBaseAdapter;

    //public LinearLayout.LayoutParams layoutParams;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_detail);
        ButterKnife.bind(this);
        //改变状态栏颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//因为不是所有的系统都可以设置颜色的，在4.4以下就不可以。。有的说4.1，所以在设置的时候要检查一下系统版本是否是4.1以上
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(R.color.trgray));
        }
        Bundle myBundle = this.getIntent().getExtras();
        id = myBundle.getString("id");
        attentionUser = myBundle.getString("otherUser");
        //Log.i(TAG, "onCreate: "+id);
        username = SpUtils.getString(DetailActivity.this,"username");
        initView();
        if(username.equals("")){
            flag = false;
            flag2 = false;
            btn_attention.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DetailActivity.this,LoginActivity.class);
                    startActivity(intent);
                    overridePendingTransition(0,0);
                }
            });
            btn_attention1.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DetailActivity.this,LoginActivity.class);
                    startActivity(intent);
                    overridePendingTransition(0,0);
                }
            });
            detail_like.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DetailActivity.this,LoginActivity.class);
                    startActivity(intent);
                    overridePendingTransition(0,0);
                }
            });
            detail_save.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DetailActivity.this,LoginActivity.class);
                    startActivity(intent);
                    overridePendingTransition(0,0);
                }
            });
        }
        isAttention(attentionUser);
        isLike(id);
        isSave(id);
        getData(id);
        getComment(id);
        getAttentionNum(id);
        scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener(){
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                //Log.i(TAG, "onScrollChange: "+scrollX+":"+scrollY);
                if(scrollY>520&&!username.equals(attentionUser)){
                    //Toast.makeText(DetailActivity.this,"123",Toast.LENGTH_SHORT).show();
                    detail_title.setVisibility(View.INVISIBLE);
                    detail_avatar1.setVisibility(View.VISIBLE);
                    detail_aurhor.setVisibility(View.VISIBLE);
                    detail_fans.setVisibility(View.VISIBLE);
                    tv_search.setVisibility(View.GONE);
                    btn_attention1.setVisibility(View.VISIBLE);
                }else {
                    detail_title.setVisibility(View.VISIBLE);
                    detail_avatar1.setVisibility(View.INVISIBLE);
                    detail_aurhor.setVisibility(View.INVISIBLE);
                    detail_fans.setVisibility(View.INVISIBLE);
                    btn_attention1.setVisibility(View.GONE);
                    tv_search.setVisibility(View.VISIBLE);
                }
            }
        });
    }
    public void initView(){
        font = Typeface.createFromAsset(getAssets(), "iconfont.ttf");
        tv_search.setTypeface(font);
        tv_search.setText(getResources().getString(R.string.font5));
        tv_share.setTypeface(font);
        tv_share.setText(getResources().getString(R.string.font10));
        detail_pen.setTypeface(font);
        detail_pen.setText(getResources().getString(R.string.pen));
        detail_comment.setTypeface(font);
        detail_comment.setText(getResources().getString(R.string.comment2));
        detail_save.setTypeface(font);
        detail_save.setText(getResources().getString(R.string.star2));
        detail_like.setTypeface(font);
        detail_like.setText(getResources().getString(R.string.like2));
        detail_bianqing.setTypeface(font);
        detail_bianqing.setText(getResources().getString(R.string.biaoqing));
        iv_back.setOnClickListener(this);
        detail_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollView.post(new Runnable() {
                    @Override
                    public void run() {
                        scrollView.scrollTo(0, detail_view.getTop() );
                    }
                });
            }
        });
        if(username.equals(attentionUser)){
            btn_attention.setVisibility(View.GONE);
            //btn_attention1.setVisibility(View.GONE);
            detail_save.setVisibility(View.GONE);
        }else{
            btn_attention.setVisibility(View.VISIBLE);
            //btn_attention1.setVisibility(View.VISIBLE);
            detail_save.setVisibility(View.VISIBLE);
        }
        detail_bianqing.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if(!flag3){
                    detail_et.clearFocus();
                    Util.showKeyBoard(detail_et);
                    edittext_nofoucs.setVisibility(View.VISIBLE);
                    edittext_hasfoucs.setVisibility(View.GONE);
                    detail_bianqing_panel.setVisibility(View.GONE);
                    flag3 = true;
                }else {
                    detail_et.clearFocus();
                    edittext_nofoucs.setVisibility(View.VISIBLE);
                    edittext_hasfoucs.setVisibility(View.GONE);
                    Util.hideKeyboard(DetailActivity.this);
                    detail_bianqing_panel.setVisibility(View.VISIBLE);
                    flag3 = false;
                }
            }
        });
        tv_search.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DetailActivity.this,SearchActivity.class);
                startActivity(intent);
                overridePendingTransition(0,0);
            }
        });
        btn_attention1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(flag){
                    cancelAttention(attentionUser);
                    btn_attention1.setText("关注");
                    btn_attention1.setBackground(getDrawable(R.drawable.buuton));
                    btn_attention1.setTextColor(getColor(R.color.white));
                    btn_attention.setText("关注");
                    btn_attention.setBackground(getDrawable(R.drawable.buuton));
                    btn_attention.setTextColor(getColor(R.color.white));
                    flag = false;
                }else{
                    attention(attentionUser);
                    btn_attention1.setText("已关注");
                    btn_attention1.setBackground(getDrawable(R.drawable.buuton2));
                    btn_attention1.setTextColor(getColor(R.color.trgray));
                    btn_attention.setText("已关注");
                    btn_attention.setBackground(getDrawable(R.drawable.buuton2));
                    btn_attention.setTextColor(getColor(R.color.trgray));
                    flag = true;
                }
            }
        });
        btn_attention.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if(flag){
                    //取消关注
                    cancelAttention(attentionUser);
                    btn_attention1.setText("关注");
                    btn_attention1.setBackground(getDrawable(R.drawable.buuton));
                    btn_attention1.setTextColor(getColor(R.color.white));
                    btn_attention.setText("关注");
                    btn_attention.setBackground(getDrawable(R.drawable.buuton));
                    btn_attention.setTextColor(getColor(R.color.white));
                    flag = false;
                }else{
                    //关注
                    attention(attentionUser);
                    btn_attention1.setText("已关注");
                    btn_attention1.setBackground(getDrawable(R.drawable.buuton2));
                    btn_attention1.setTextColor(getColor(R.color.trgray));
                    btn_attention.setText("已关注");
                    btn_attention.setBackground(getDrawable(R.drawable.buuton2));
                    btn_attention.setTextColor(getColor(R.color.trgray));
                    flag = true;
                }
            }
        });
        detail_save.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if(flag1){
                    //取消收藏
                    cancelSave(id);
                    detail_save.setTextColor(getColor(R.color.unsaved));
                    flag1 = false;
                }else{
                    //收藏
                    save(id);
                    detail_save.setTextColor(getColor(R.color.saved));
                    flag1 = true;
                }
            }
        });
        detail_like.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if(flag2){
                    //取消点赞
                    cancelLike(id);
                    detail_like.setTextColor(getColor(R.color.unsaved));
                    flag2 = false;
                }else{
                    //点赞
                    like(id);
                    detail_like.setTextColor(getColor(R.color.colorAccent));
                    flag2 = true;
                }
            }
        });
        detail_et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    detail_bianqing_panel.setVisibility(View.GONE);
                    flag3 = true;
                    //Util.hideKeyboard(DetailActivity.this);
                    edittext_nofoucs.setVisibility(View.VISIBLE);
                    edittext_hasfoucs.setVisibility(View.GONE);

                }else{
                    edittext_nofoucs.setVisibility(View.GONE);
                    edittext_hasfoucs.setVisibility(View.VISIBLE);
                }
            }
        });
        btn_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                detail_et.clearFocus();
                Util.hideKeyboard(DetailActivity.this);
                String content = detail_et.getText().toString();
                String author = list.get(0).getUsername();
                detail_no_comment.setVisibility(View.GONE);
                detail_comment_list.setVisibility(View.VISIBLE);
                getUserInfo(content,author);
            }
        });
        for(int i = 0;i<emojis.length;i++){
            listEmoji.add(emojis[i]);
        }
        MyEmojiAdapter myEmojiAdapter = new MyEmojiAdapter(listEmoji);
        detail_biaoqing_gridview.setAdapter(myEmojiAdapter);
        detail_biaoqing_gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView tv = view.findViewById(R.id.item_emoji);
                detail_et.setText(detail_et.getText().append(tv.getText().toString().trim()));
            }
        });
    }
    public void getUserInfo(String content,String author){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        Call<ResponseBody> call = responseApi.getInfoByUsername1(username);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try {
                    json = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Log.i(TAG, "onResponse: 123"+json);
                JsonObject obj = (JsonObject) new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                if(code == 200&&obj.has("avatar")&&obj.has("nickname")){
                    String avatar = obj.get("avatar").getAsString();
                    String nickname = obj.get("nickname").getAsString();
                    Log.i(TAG, "onResponse: "+avatar);
                    Log.i(TAG, "onResponse: "+nickname);
                    submitComment(content,author,avatar,nickname);
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onFailure: "+t);
            }
        });
    }
    public void getComment(String newId){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        Call<ResponseBody> call = responseApi.getComment(newId);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try {
                    json = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Log.i(TAG, "onResponse: "+json);
                JsonObject obj = (JsonObject) new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                if(code ==200&&obj.has("data")){
                    JsonArray objArr = obj.getAsJsonArray("data");
                    Gson gson = new Gson();
                    commentList = gson.fromJson(objArr,new TypeToken<ArrayList<Comment>>(){}.getType());
                    detail_comment_list.setVisibility(View.VISIBLE);
                    detail_no_comment.setVisibility(View.GONE);
                    myCommentBaseAdapter = new MyCommentBaseAdapter(commentList);
                    detail_comment_list.setAdapter(myCommentBaseAdapter);
                }else{
                    detail_no_comment.setVisibility(View.VISIBLE);
                    detail_comment_list.setVisibility(View.GONE);
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onFailure: "+t);
            }
        });
    }
    public void submitComment(String content,String author,String avatar,String nickname){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        Call<ResponseBody> call = responseApi.submitComment(username,id,content,author,avatar,nickname);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try {
                    json = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Log.i(TAG, "onResponse: "+json);
                JsonObject obj = (JsonObject) new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                if(code == 200 &&obj.has("data")){
                    Toasty.success(DetailActivity.this,"发表成功",Toasty.LENGTH_SHORT).show();
                    detail_et.setText("");
                    Util.hideKeyboard(DetailActivity.this);
                    JsonObject newObj = obj.get("data2").getAsJsonObject();
                    Comment comment = new Comment();
                    comment.set_id(newObj.get("_id").getAsString());
                    comment.set__v(newObj.get("__v").getAsInt());
                    comment.setAvatar(newObj.get("avatar").getAsString());
                    comment.setAuthor(newObj.get("author").getAsString());
                    comment.setContent(newObj.get("content").getAsString());
                    comment.setNewId(newObj.get("newId").getAsString());
                    comment.setNickname(newObj.get("nickname").getAsString());
                    comment.setTime(newObj.get("time").getAsString());
                    comment.setUsername(newObj.get("username").getAsString());
                    commentList.add(comment);
                    Log.i(TAG, "onResponse: "+myCommentBaseAdapter);
                    if(myCommentBaseAdapter!=null){
                        myCommentBaseAdapter.notifyDataSetChanged();
                    }else {
                        MyCommentBaseAdapter myCommentBaseAdapter1 = new MyCommentBaseAdapter(newList2);
                        detail_comment_list.setAdapter(myCommentBaseAdapter1);
                        newList2.add(comment);
                        myCommentBaseAdapter1.notifyDataSetChanged();
                    }
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onFailure: "+t);
            }
        });
    }
    public void getAttentionNum(String username){
        Log.i(TAG, "getAttentionNum: "+username);
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        Call<ResponseBody> call = responseApi.getAttentionNum(username);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try {
                    json = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Log.i(TAG, "onResponse: "+json);
                JsonObject obj = (JsonObject) new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                if(code==200){
                    String attentionNum = obj.get("data").getAsString();
                    detail_fans.setText(attentionNum+"粉丝");
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onFailure: "+t);
            }
        });
    }
    public void save(String newId){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        Call<ResponseBody> call = responseApi.save(newId,username);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try{
                    json = response.body().string();
                    //Log.i(TAG, "onResponse: "+json);
                }catch (Exception e){
                    e.printStackTrace();
                }
                Log.i(TAG, "onResponse: "+json);
                JsonObject obj = (JsonObject)new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                if(code ==200&&obj.has("data")){
                    Toasty.success(DetailActivity.this,"收藏成功",Toasty.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }
    public void cancelSave(String newId){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        Call<ResponseBody> call = responseApi.cancelSave(newId,username);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try{
                    json = response.body().string();
                    //Log.i(TAG, "onResponse: "+json);
                }catch (Exception e){
                    e.printStackTrace();
                }
                Log.i(TAG, "onResponse: "+json);
                JsonObject obj = (JsonObject)new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                if(code ==200&&obj.has("data")){
                    Toasty.success(DetailActivity.this,"取消收藏",Toasty.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onFailure: "+t);
            }
        });
    }
    public void like(String newId){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        Call<ResponseBody> call = responseApi.like(newId,username);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try{
                    json = response.body().string();
                    //Log.i(TAG, "onResponse: "+json);
                }catch (Exception e){
                    e.printStackTrace();
                }
                Log.i(TAG, "onResponse: "+json);
                JsonObject obj = (JsonObject)new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                if(code ==200&&obj.has("data")){
                    Toasty.success(DetailActivity.this,"点赞成功",Toasty.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onFailure: "+t);
            }
        });
    }
    public void cancelLike(String newId){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        Call<ResponseBody> call = responseApi.cancelLike(newId,username);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try{
                    json = response.body().string();
                    //Log.i(TAG, "onResponse: "+json);
                }catch (Exception e){
                    e.printStackTrace();
                }
                Log.i(TAG, "onResponse: "+json);
                JsonObject obj = (JsonObject)new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                if(code ==200&&obj.has("data")){
                    Toasty.success(DetailActivity.this,"取消点赞",Toasty.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onFailure: "+t);
            }
        });
    }
    public void attention(String attentionUser){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        Call<ResponseBody> call = responseApi.attention(attentionUser,username);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try{
                    json = response.body().string();
                    //Log.i(TAG, "onResponse: "+json);
                }catch (Exception e){
                    e.printStackTrace();
                }
                JsonObject obj = (JsonObject)new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                if(code == 200&&obj.has("data")){
                    Toasty.success(DetailActivity.this,"关注成功",Toasty.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onFailure: "+t);
            }
        });
    }
    public void cancelAttention(String attentionUser){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        Call<ResponseBody> call = responseApi.cancelAttention(attentionUser,username);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try{
                    json = response.body().string();
                    //Log.i(TAG, "onResponse: "+json);
                }catch (Exception e){
                    e.printStackTrace();
                }
                JsonObject obj = (JsonObject)new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                if(code == 200&&obj.has("data")){
                    Toasty.success(DetailActivity.this,"取消关注",Toasty.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onFailure: "+t);
            }
        });
    }
    public void isAttention(String attentionUser){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        Call<ResponseBody> call = responseApi.isAttention(attentionUser,username);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try{
                    json = response.body().string();
                    //Log.i(TAG, "onResponse: "+json);
                }catch (Exception e){
                    e.printStackTrace();
                }
                JsonObject obj = (JsonObject)new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                Integer data = obj.get("data").getAsInt();
                if(code ==200 && data == 0){
                    flag = false;
                    btn_attention1.setText("关注");
                    btn_attention1.setBackground(getDrawable(R.drawable.buuton));
                    btn_attention1.setTextColor(getColor(R.color.white));
                    btn_attention.setText("关注");
                    btn_attention.setBackground(getDrawable(R.drawable.buuton));
                    btn_attention.setTextColor(getColor(R.color.white));
                }else if(code == 200 && data == 1){
                    flag = true;
                    btn_attention1.setText("已关注");
                    btn_attention1.setBackground(getDrawable(R.drawable.buuton2));
                    btn_attention1.setTextColor(getColor(R.color.trgray));
                    btn_attention.setText("已关注");
                    btn_attention.setBackground(getDrawable(R.drawable.buuton2));
                    btn_attention.setTextColor(getColor(R.color.trgray));
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onFailure: "+t);
            }
        });
    }
    public void isLike(String id){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        Call<ResponseBody> call = responseApi.isLike(id,username);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try{
                    json = response.body().string();
                    //Log.i(TAG, "onResponse: "+json);
                }catch (Exception e){
                    e.printStackTrace();
                }
                //Log.i(TAG, "onResponse: "+json);
                JsonObject obj = (JsonObject)new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                Integer data = obj.get("data").getAsInt();
                if(code == 200 && data ==0){
                    flag2 = false;
                    detail_like.setTextColor(getColor(R.color.unsaved));
                }else{
                    flag2 = true;
                    detail_like.setTextColor(getColor(R.color.colorAccent));
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onFailure: "+t);
            }
        });
    }
    public void isSave(String id){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        Call<ResponseBody> call = responseApi.isSave(id,username);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try{
                    json = response.body().string();
                    //Log.i(TAG, "onResponse: "+json);
                }catch (Exception e){
                    e.printStackTrace();
                }
               // Log.i(TAG, "onResponse: "+json);
                JsonObject obj = (JsonObject)new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                Integer data = obj.get("data").getAsInt();
                if(code == 200&& data == 0){
                    flag1 = false;
                    detail_save.setTextColor(getColor(R.color.unsaved));
                }else{
                    flag1 = true;
                    detail_save.setTextColor(getColor(R.color.saved));
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onFailure: "+t);
            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.detail_back:
                //不能使用intent 会重新刷新activity
                finish();
                break;
        }
    }
    private void getData(String id){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        Call<ResponseBody> call = responseApi.getNewsById(id);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try{
                    json = response.body().string();
                    //Log.i(TAG, "onResponse: "+json);
                }catch (Exception e){
                    e.printStackTrace();
                }
                JsonObject obj = (JsonObject)new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                if(code == 200 && obj.has("data")){
                    JsonArray objArr = obj.getAsJsonArray("data");
                    Gson gson = new Gson();
                    list = gson.fromJson(objArr,new TypeToken<ArrayList<News>>(){}.getType());
                    detail_conetent_title.setText(list.get(0).getTitle());
                    detail_user.setText(list.get(0).getAuthor());
                    detail_aurhor.setText(list.get(0).getAuthor());
                    detail_time.setText(list.get(0).getTime());
                    getAttentionNum(list.get(0).getUsername());

                    RequestOptions requestOptions = new RequestOptions();
                    requestOptions.placeholder(R.drawable.boy);
                    String imgUrl1 = baseUrl+"public/images/"+list.get(0).getAvatar()+".jpg";
                    Glide.with(DetailActivity.this).load(imgUrl1).apply(requestOptions).into(detail_avatar);
                    Glide.with(DetailActivity.this).load(imgUrl1).apply(requestOptions).into(detail_avatar1);
                    //detail_content.setText(list.get(0).getContent());
                    String arrText[] = list.get(0).getContent().split("&");
                    String arrImage[] = list.get(0).getImages().split("&");
                    Log.i(TAG, "onResponse: "+arrText.length);
                    Log.i(TAG, "onResponse: "+arrImage.length);
                    RequestOptions requestOption = new RequestOptions();
                    requestOption.placeholder(R.drawable.placeholder).error(R.drawable.error_img);
                    if(arrText.length==1){
                        detail_conetent_title.setVisibility(View.GONE);
                        final TextView textView1 = new TextView(DetailActivity.this);
                        textView1.setText(arrText[0]);
                        textView1.setTextSize(18);
                        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                        layoutParams.setMargins(0, 50, 0, -30);//4个参数按顺序分别是左上右下
                        linearLayout.addView(textView1);
                        for(int i =0;i<arrImage.length;i++) {
                            String imgUrl = baseUrl + "public/images/" + arrImage[i] + ".jpg";
                            final ImageView imageView1 = new ImageView(DetailActivity.this);
                            //设置图片大小缩放
                            imageView1.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                            imageView1.setLayoutParams(layoutParams);
                            imageView1.setScaleType(ImageView.ScaleType.MATRIX);
                            //imageView.setLayoutParams(linearParams);
                            Glide.with(DetailActivity.this).load(imgUrl).apply(requestOption).into(imageView1);
                            linearLayout.addView(imageView1);
                        }
                    }else{
                        for(int i=0;i<arrImage.length;i++){
                            final TextView textView = new TextView(DetailActivity.this);
                            textView.setText(arrText[i]);
                            textView.setTextSize(18);
                            //textView的间距
                            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                            layoutParams.setMargins(0, 50, 0, 30);//4个参数按顺序分别是左上右下
                            //textView.setLayoutParams(layoutParams);
                            String imgUrl = baseUrl+"public/images/"+arrImage[i]+".jpg";
                            final ImageView imageView = new ImageView(DetailActivity.this);
                            //设置图片大小缩放
                            imageView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                            imageView.setLayoutParams(layoutParams);
                            imageView.setScaleType(ImageView.ScaleType.MATRIX );
                            //imageView.setLayoutParams(linearParams);
                            Glide.with(DetailActivity.this).load(imgUrl).apply(requestOption).into(imageView);
                            linearLayout.addView(textView);
                            linearLayout.addView(imageView);
                        }
                        final TextView textView1 = new TextView(DetailActivity.this);
                        textView1.setText(arrText[arrText.length-1]);
                        textView1.setTextSize(18);
                        linearLayout.addView(textView1);
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onFailure: 出错了");
            }
        });

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {//如果返回键按下
            detail_bianqing_panel.setVisibility(View.GONE);
            detail_et.clearFocus();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
